<?php
namespace Admin\Controller;
use Think\Controller;
class IndexController extends Controller {
   public function _initialize()  
    {  
		header("Content-Type: text/html; charset=utf-8");

    }  
    public function index(){
		$this->display('login');
	}
	public function Login(){
		$m = M('user');
		$phone = $_POST['u_phone'];
		//加密登陆处理
/*		$result=passwd($_POST['u_phone'],$_POST['u_pass']);
		if($result['code']==1){
			$Login=$result['data'];
		}
		else{
			$this->error("手机号或邀请码不能为空");

		}*/
		/********注销线**************************/
		$pass = $_POST['u_pass'];
		if($phone == '' || $pass == ''){
			$this->error("手机号或邀请码不能为空");
		}
		$where_user['u_phone']=$phone;
		$where_user['u_pass']=$pass;
		$Login = $m->where($where_user)->find();
		/********注销线**************************/
		if($Login){
			
			switch ($Login['u_type']){
				case 0:
					session('user',$Login);
					$this->success('登陆成功！','admin_user');
					break;  
				case 1:
					session('user',$Login);
					$this->success('登陆成功！','diqu_weixin');
					break;  
				case 2:
					session('zi_user',$Login);
					$this->success('登陆成功！',U('Managezi/manage_zizhanghao'));
					break;  
				case 3:
					session('user',$Login);
					$this->success('登陆成功！',U('Manageme/manage_me'));
					break;
				default:
			}
			
		}else{
			$this->error('账号或者密码错误');
		}
		
	}

    /**
     * 退出
    */
    public function loginout(){
    		$message=D('User')->loginout(1);
			session(null);
			$this->success($message,U('index/index'));
			die;
    }

	 public function chengshi()
    {
    	if(!session('user')){
    		$message=D('User')->loginout(2);
    		$this->success($message,U('index/index'));die;
    	}
        if (IS_POST) {
            $parent_id['PARENT_ID'] = I('post.pro_id','addslashes');
			$region = M('region')->where($parent_id)->select();
			foreach($region as $key=>$val){
				$opt .= "<option value='{$val['id']}'>{$val['region_name']}</option>";
			 }
			 echo json_encode($opt);
        } else {
            $parent_id['REGION_TYPE'] = 1;
            $region = M('region')->where($parent_id)->select();
            $this->assign('region',$region);

            $this->display('add_local');
        }
    }
	/* 添加 */
	function user_add(){
    	if(!session('user')&&!session('zi_user')){
    		$message=D('User')->loginout(2);
    		$this->success($message,U('index/index'));die;
    	}
		$user = $_POST;
		$m = M('user');
		$date = date("Y-m-d H:i:s");
		$user['code']=rand(10000,99999);
		$user['u_time'] = $date;
		if($_POST['u_pro_id']||$_POST['u_city_id']||$_POST['u_area_id']){
				$user['p_id']=$_SESSION['user']['u_id'];
				$user['wechat_id']=$_SESSION['user']['wechat_id'];
		}
		else{
			if($_SESSION['user']){
				$user['u_pro_id']=$_SESSION['user']['u_pro_id'];
				$user['u_city_id']=$_SESSION['user']['u_city_id'];
				$user['u_area_id']=$_SESSION['user']['u_area_id'];
				$user['p_id']=$_SESSION['user']['u_id'];
				$user['wechat_id']=$_SESSION['user']['wechat_id'];
			}
			elseif ($_SESSION['zi_user']) {
				$user['u_pro_id']=$_SESSION['zi_user']['u_pro_id'];
				$user['u_city_id']=$_SESSION['zi_user']['u_city_id'];
				$user['u_area_id']=$_SESSION['zi_user']['u_area_id'];
				$user['p_id']=$_SESSION['zi_user']['u_id'];
				$user['wechat_id']=$_SESSION['zi_user']['wechat_id'];

			}
		}
		$info = $m->data($user)->add();
		if($info){
/*			$my_user= $m->where("u_id=$info")->find();
			$user_update['u_pass']=md5(md5($_POST['u_pass']).$my_user['code']);//MD5加密
			$m->where("u_id=$info")->save($user_update);*/
			$message="恭喜".$_POST['u_name']."成为评估令用户";
			//$this->ajaxReturn($message);

			if($_POST['u_type']==3&&$_POST['ck_aj']==0){
				$where['wechat_id']=$_SESSION['zi_user']['wechat_id'];
				$wechat=M('wechat')->where($where)->find();
				$appid=$wechat['appid'];
				$appsecret=$wechat['appsecret'];
				$api=new \Com\WechatAuth($appid,$appsecret);
				$data_str="$info,ajy";//设置按揭员处理
				$res=$api->textCreat($data_str);
				$img_link="https://mp.weixin.qq.com/cgi-bin/showqrcode?ticket=".$res['ticket'];
				if($res){
					$my_msg['code']=1;
					$my_msg['message']='添加成功';
					$my_msg['data']=$img_link;
				}
				else{
					$my_msg['code']=2;
					$my_msg['message']='添加失败';
					$my_msg['data']=$img_link;
				}
				send_email($_POST['u_email'],$message);
				$this->ajaxReturn($my_msg);
			}
			$this->ajaxReturn('ok');
		}else{
			$this->ajaxReturn('error');
		}
	}
	/**
	 * 公众号列表删除管理
	 * @return [type] [description]
	 */
	public function admin_user(){
		header("Content-Type: text/html; charset=utf-8");
    	if(!session('user')){
    		$message=D('User')->loginout(2);
    		$this->success($message,U('index/index'));
    		die;
    	}
		if(!$_GET['p'])$_get['p']=1;
		$data = M('user');
		$where['u_type']=1;
		$count      = $data->where($where)->count();
		$Page       = new \Think\Page($count,10);
		$show       = $Page->show();
		$list = $data->limit($Page->firstRow.','.$Page->listRows)->where($where)->select();
		$this->assign('count',$count);
		$this->assign('list',$list);
		$this->assign('page',$show);
		$this->display('manage');
	}
	public function zizhanghao(){
    	if(!session('user')){
    		$message=D('User')->loginout(2);
    		$this->success($message,U('index/index'));
    		die;
    	}
		$this->display('Managezi/manage_zizhanghao');
	}
	/**
	 * 微信绑定公众号重新处理删除操作
	 * @DateTime:    [2018-03-26 11:35:49]
	 * @return [type] [description]
	 */
	public function diqu_weixin(){
    	if(!session('user')){
    		$message=D('user')->loginout(2);
    		$this->success($message,U('index/index'));
    		die;
    	}
        $db_data=M('wechat');
        if($_POST){
        	$db_user=M('user');
        	$db_web_user=M('web_user');
                $del=$_POST['id_arr'];
                foreach ($_POST['id_arr'] as $key => $value) {
                    $del_arr=$value.$del_arr;
                    if($_POST['id_arr'][$key+1]){
                        $del_arr=','.$del_arr;
                    }
                }
                $db_data->startTrans();
                $db_user->startTrans();
                $db_web_user->startTrans();
                $where_wechat_id_arr['wechat_id']=array('IN',$del_arr);
                $res_db_data=$db_data->delete($del_arr);
                $res_db_user=$db_user->where($where_wechat_id_arr)->delete();
                $res_db_web_user=$db_web_user->where($where_wechat_id_arr)->delete();
                if($res_db_data!=false&&$res_db_user!=false&&$res_db_web_user!=false){
                	$db_data->commit();
                	$db_user->commit();
                	$db_web_user->commit();
                    $this->success('删除成功',U('diqu_weixin'),1);die;
                }
                else{
                	$db_data->rollback();
                	$db_user->rollback();
                	$db_web_user->rollback();
                    $this->error('删除失败',U('diqu_weixin'),1);die;
                }
        }
        else{
            if(!$_GET['p'])$_get['p']=1;
            $count      = $db_data->count();
            $Page       = new \Think\Page($count,5);
            $show       = $Page->show();
			$wherex['u_pro_id'] = $_SESSION['user']['u_pro_id'];
			$wherex['u_city_id'] = $_SESSION['user']['u_city_id'];
			$wherex['p_id'] = $_SESSION['user']['u_id'];
			$wherex['u_type'] = 2;
			$str='a.u_city_id='.$_SESSION['user']['u_city_id'].' and a.u_pro_id='.$_SESSION['user']['u_pro_id'].' and p_id='.$_SESSION['user']['u_id'].' and u_type=2';
            $list = $db_data 
            ->alias('a')
            ->join('bm_user b ON a.wechat_id=b.wechat_id','LEFT')
            ->field('a.*, b.u_id')
            ->where('a.u_city_id='.$_SESSION['user']['u_city_id'].' and a.u_pro_id='.$_SESSION['user']['u_pro_id'].' and p_id='.$_SESSION['user']['u_id'].' and u_type=2')
            ->limit($Page->firstRow.','.$Page->listRows)
            ->select();
            $this->assign('count',$count);
            $this->assign('list',$list);
            $this->assign('page',$show);
        }
		$this->display('manage_diqu_weixin');
	}
	public function nav(){
    	if(!session('user')){
    		$message=D('User')->loginout(2);
    		$this->success($message,U('index/index'));
    		die;
    	}
		$this->display('zhong_nav');
	}
	/**
	 * 添加微信公众账号以及绑定微信
	 * 		
	 */
	public function add_weixin(){
		if($_POST){
			$msg = D("Wxauthorise")->add_wechat();
			$this->ajaxReturn($msg);
		}
		else{
			$this->display();
		}
		
	}
	/**
	 * 发送验证码。ajax
	 * @return [type] [description]
	 */
	public function send_chack(){
		if(IS_AJAX){
			$res=send_chack($_POST['phone']);
			if($res['message']==1){
				return json_encode($res);
			}
			else{
				return 'error';
			}
		}
		return '传输方式有误';
	}

	 /**
	 * 手机验证,
	 */
	public function chack_phone(){
		if($_POST){
			$res=chack($_POST);
			if($res==1){
				return 'success';
			}
			elseif($res==2){ 
				return 'error';
			}
		}
		return 'error';
	}
	/**
	 * 删除用户
	 */
	public function del_user(){
		if($_POST){
			$del=$_POST['id_arr'];
			foreach ($_POST['id_arr'] as $key => $value) {
				$del_arr=$value.$del_arr;
				if($_POST['id_arr'][$key+1]){
					$del_arr=','.$del_arr;
				}
			}
			$res=M('user')->delete($del_arr);
			if($res){
				$this->success('删除成功',U('index/admin_user'),1);
			}
			else{
				$this->error('删除失败',U('index/admin_user'),1);
			}
		}
		else{
			
		}
	}
	/**
	 * 密码更新
	 * @return [type] [description]
	 */
	public function update_passwd(){
		if($_POST){
			if($_POST['old_password']==$_POST['new_password']){
				$this->error('新旧密码不可以相同');
			}
			elseif ($_POST['old_password']&&$_POST['new_password']) {
				$user=$_SESSION['user'];
				/**
				 * 加密
				 */
				//$passwd['u_pass']=md5(md5($_POST['new_password']).$user['code']);
				/**
				 * 不加密
				 */
				$passwd['u_pass']=$_POST['new_password'];
				$where['u_id']=$user['u_id'];
				$res=M('user')->where($where)->save($passwd);
				if($res){
					$message=D('User')->loginout(3);
					session(null);
					$this->success($message,U('index/index'));die;
				}
				else{
					$this->error('更新失败');

				}

			}
			else{
				$this->error('请检查新旧密码是否填写');
			}
		}
		else{
			$this->display();
		}
	}
	/**
	 * 传输一个省国标码，返回该省已使用地区
	 * @DateTime:    [2018-03-10 12:14:48]
	 */
	public function ajax_gb_area(){
		if(IS_AJAX){
			$result=M('user')
					->alias('a')
					->join('bm_region b ON a.u_city_id=b.ID','left')
					->field('b.region_id,b.id')
					->select();
			if($result){
				$msg['code']=1;
				$msg['message']='查询成功';
				$msg['data']=$result;
			}
			else{
				$msg['code']=2;
				$msg['msg']='没有数据';
				$msg['data']=0;
			}
		}
		else{
			$msg['code']=3;
			$msg['message']='请求方式失败';
		}
		$this->ajaxReturn($msg);
	}
	/**
	 * ajax上传文件验证微信文件
	 * @DateTime:    [2018-03-20 16:08:34]
	 */
	public function upload_push(){
	      $file = $_FILES['file'];
	      $name = $file['name'];
	      $type = strtolower(substr($name,strrpos($name,'.')+1));
	      $allow_type = array('jpg','jpeg','gif','png','txt'); //
	      if(!in_array($type, $allow_type)){
	        $mes['code']=2;
	        $mes['message']="文件类型不是允许上传类型";
	      }
	      if(!is_uploaded_file($file['tmp_name'])){
	        $mes['code']=6;
	        $mes['message']="不是通过HTTP POST上传";
	      }
	      $upload_path ="Uploads/wechat";
	      $new_p=$name.".".$type;
	      $res=move_uploaded_file($file['tmp_name'],$new_p);
	      if($res){
	        $mes['code']=1;
	        $mes['link']="/".$new_p;
	        $msg['message']='成功';
	        return json_encode($mes);
	      
	      } 
	      else{
	        $mes['code']=996;
	        $mes['link']="/".$new_p;
	        $msg['message']='添加失败';
	        return json_encode($mes);

	      }
	}
}




































































